<template>
  <van-dialog class="sign-dialog" :class="[{'horiz': isHorizontal}, {'full': fullScreen}]" :value="show" :showConfirmButton="false">
    <div class="sign-layout" v-if="show">
      <div class="icon-close" @click="handleClose">
        <van-icon name="cross" color="#000" size="22" />
      </div>
      <div class="tips">请在下方空白区域横向书写签名</div>
      <div ref="SIGNBOX" class="sign-box" v-if="show">
        <SignCanvas class="c-canvas" v-if="showSignCanvas" v-model="signImgData" ref="SIGN" :options="options" />
      </div>

      <div class="btns">
        <van-button :disabled="sbLoading" plain @click="handleClearCanvas">清除</van-button>
        <van-button type="primary" :loading="sbLoading" loading-text="提交中..." @click="handleSaveImage">确认</van-button>
      </div>
    </div>
  </van-dialog>
</template>

<script>
import SignCanvas from './signCanvas'
import { Toast } from 'vant'
export default {
  name: 'sign-dialog',
  components: { SignCanvas },
  props: {
    show: Boolean,
    isRenewal: String
  },
  data() {
    return {
      sbLoading: false,
      showSignCanvas: false,
      signImgData: null,
      // isFullScreen: true,
      isHorizontal: false,
      options: {
        bgColor: '#fff',
        writeColor: '#000',
        isSign: true,
        writeWidth: 5,
        isFullScreen: true,
        canvasWidth: document.body.clientWidth, //canvas宽高 [Number] 可选
        canvasHeight: document.body.clientHeight, //高度  [Number] 可选
      }
    }
  },
  mounted() {
    this.init()
    window.addEventListener('resize', this.renderResize)
  },
  destroyed() {
    window.removeEventListener('resize', this.renderResize)
  },
  computed: {
    fullScreen() {
      const { isHorizontal } = this
      return isHorizontal ? false : true
    }
  },
  methods: {
    init() {
      this.setHorizontalState()
      this.resetCanvas()
    },

    resetCanvas() {
      this.signImgData = null
      this.showSignCanvas = false
      const showCanvasFun = () => {
        this.options.isFullScreen = this.fullScreen
        this.$nextTick(() => {
          this.showSignCanvas = true
        })
      }
      setTimeout(showCanvasFun, 200)
    },

    handleClearCanvas() {
      this.showSignCanvas = false
      this.$nextTick(() => {
        this.showSignCanvas = true
      })
      this.$refs.SIGN?.canvasClear()
    },
    handleSaveImage() {
      if (!this.signImgData) {
        Toast('请签字后再提交')
        return 
      }
      this.handleFinish()
    },
    handleFinish() {
      this.$emit('ok',this.signImgData)
      this.handleClose()
    },
    handleClose() {
      this.$emit('update:show', false)
      this.resetCanvas()
    },

    setHorizontalState() {
      // const width = document.documentElement.clientWidth
      // const height = document.documentElement.clientHeight
      // this.isHorizontal = width > height
      this.isHorizontal = true
    },

    renderResize() {
      this.setHorizontalState()
      this.resetCanvas()
    }
  }
}
</script>
<style lang="scss" scoped>
.sign-layout {
  padding: 10px;
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 300px;
  .icon-close {
    position: absolute;
    right: 15px;
    top: 10px;

    display: flex;
    align-items: center;
    justify-content: center;
  }
  .title {
    text-align: center;
    color: 333;
    padding-bottom: 5px;
  }
  .tips {
    text-align: center;
    color: #999;
    padding: 10px;
    flex-shrink: 0;
  }
  .sign-box {
    flex: 1;
    display: flex;
    // border: 1px solid #ccc;
    overflow: hidden;

    .c-canvas {
      flex: 1;
    }
  }
  .btns {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding-top: 15px;
    flex-shrink: 0;

    .btn {
      min-width: 20%;
      border-radius: 10px;
      height: 40px;
    }
    .btn-clear {
      border: 1px solid #f61f1e;
      color: #f61f1e;
    }
    .btn-ok {
      background: linear-gradient(180deg, #da2f0a 0%, #a80000 100%);
      border: none;
    }
  }
}

.full {
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  transform: initial;

  .sign-layout {
    transform: rotate(90deg)
      translate(calc((100vh - 100vw) / 2), calc((100vh - 100vw) / 2));
    transform-origin: center center;
    width: 100vh;
    height: 100vw;
    padding: 10px;
    box-sizing: border-box;
  }
}

.horiz {
  width: 100vw;
  height: 100vh;
  top: 50%;
  .sign-layout {
    width: 100%;
    height: 100vh;
    padding: 2vh;
    min-height: initial;
    box-sizing: border-box;
  }
  .icon-close {
    top: 4vh;
  }
  .tips {
    padding: 3vh 0;
    box-sizing: border-box;
    // height: 8vh;
    box-sizing: border-box;
  }
  .btns {
    padding-top: 3vh;
    .btn {
      height: 14vh;
    }
  }
}
</style>
